<?php

use App\Http\Controllers\ProfileController;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

// 首页路由：所有用户都可以访问，显示帖子列表
// 已登录用户：显示推荐/关注等个性化内容
// 未登录用户：显示热门/最新等公开内容
Route::get('/', [\App\Http\Controllers\HomeController::class, 'index'])->name('home');

Route::middleware('auth')->group(function () {
    // 个人中心路由（完整功能：头像、昵称、签名、简介等）
    Route::get('/profile', [\App\Http\Controllers\UserController::class, 'show'])->name('profile.show');
    Route::put('/profile', [\App\Http\Controllers\UserController::class, 'update'])->name('profile.update');
    
    // 账户设置路由（密码修改、删除账户）
    Route::get('/profile/settings', [ProfileController::class, 'edit'])->name('profile.settings');
    Route::patch('/profile/settings', [ProfileController::class, 'update'])->name('profile.settings.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

// 用户公开主页（无需认证）
Route::get('/users/{user}', [\App\Http\Controllers\UserController::class, 'showPublic'])->name('users.show');

// 搜索功能（公开，但可能需要认证才能查看某些结果）
Route::get('/search', [\App\Http\Controllers\SearchController::class, 'index'])->name('search.index');

// 关注功能（需要认证）
Route::middleware('auth')->group(function () {
    Route::post('/users/{user}/follow', [\App\Http\Controllers\FollowController::class, 'toggle'])->name('users.follow');
    Route::get('/users/{user}/following', [\App\Http\Controllers\FollowController::class, 'following'])->name('users.following');
    Route::get('/users/{user}/followers', [\App\Http\Controllers\FollowController::class, 'followers'])->name('users.followers');
    
    // 通知功能
    Route::get('/notifications', [\App\Http\Controllers\NotificationController::class, 'index'])->name('notifications.index');
    Route::post('/notifications/{id}/read', [\App\Http\Controllers\NotificationController::class, 'markAsRead'])->name('notifications.markAsRead');
    Route::post('/notifications/read-all', [\App\Http\Controllers\NotificationController::class, 'markAllAsRead'])->name('notifications.markAllAsRead');
    Route::get('/notifications/unread-count', [\App\Http\Controllers\NotificationController::class, 'unreadCount'])->name('notifications.unreadCount');
});

// 贴吧路由（部分需要认证，部分公开）
Route::resource('bars', \App\Http\Controllers\BarController::class);
Route::middleware('auth')->group(function () {
    Route::post('/bars/{bar}/join', [\App\Http\Controllers\BarController::class, 'join'])->name('bars.join');
    Route::post('/bars/{bar}/leave', [\App\Http\Controllers\BarController::class, 'leave'])->name('bars.leave');
    
    // 帖子路由（嵌套在贴吧下）
    Route::get('/bars/{bar}/posts', [\App\Http\Controllers\PostController::class, 'index'])->name('posts.index');
    Route::get('/bars/{bar}/posts/create', [\App\Http\Controllers\PostController::class, 'create'])->name('posts.create');
    Route::post('/bars/{bar}/posts', [\App\Http\Controllers\PostController::class, 'store'])->name('posts.store');
});

// 帖子详情和其他操作（不需要嵌套在贴吧路径下，使用 slug 或 id）
Route::get('/posts/{post}', [\App\Http\Controllers\PostController::class, 'show'])->name('posts.show');
Route::middleware('auth')->group(function () {
    Route::get('/posts/{post}/edit', [\App\Http\Controllers\PostController::class, 'edit'])->name('posts.edit');
    Route::put('/posts/{post}', [\App\Http\Controllers\PostController::class, 'update'])->name('posts.update');
    Route::delete('/posts/{post}', [\App\Http\Controllers\PostController::class, 'destroy'])->name('posts.destroy');
    
    // 帖子互动操作
    Route::post('/posts/{post}/like', [\App\Http\Controllers\PostController::class, 'toggleLike'])->name('posts.like');
    Route::post('/posts/{post}/favorite', [\App\Http\Controllers\PostController::class, 'toggleFavorite'])->name('posts.favorite');
    
    // 帖子管理操作（仅吧主）
    Route::post('/posts/{post}/pin', [\App\Http\Controllers\PostController::class, 'togglePin'])->name('posts.pin');
    Route::post('/posts/{post}/essence', [\App\Http\Controllers\PostController::class, 'toggleEssence'])->name('posts.essence');
    
    // 回复路由（嵌套在帖子下）
    Route::post('/posts/{post}/replies', [\App\Http\Controllers\ReplyController::class, 'store'])->name('replies.store');
    Route::put('/replies/{reply}', [\App\Http\Controllers\ReplyController::class, 'update'])->name('replies.update');
    Route::delete('/replies/{reply}', [\App\Http\Controllers\ReplyController::class, 'destroy'])->name('replies.destroy');
    Route::post('/replies/{reply}/like', [\App\Http\Controllers\ReplyController::class, 'toggleLike'])->name('replies.like');
    
    // 举报功能
    Route::get('/reports/create', [\App\Http\Controllers\ReportController::class, 'create'])->name('reports.create');
    Route::post('/reports', [\App\Http\Controllers\ReportController::class, 'store'])->name('reports.store');
});

// 管理员后台（需要认证且是管理员）
Route::middleware(['auth'])->prefix('admin')->name('admin.')->group(function () {
    Route::get('/reports', [\App\Http\Controllers\AdminController::class, 'reports'])->name('reports.index');
    Route::post('/reports/{report}/process', [\App\Http\Controllers\AdminController::class, 'processReport'])->name('reports.process');
});

require __DIR__.'/auth.php';
